home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Demos / A.D. Software / OOFILE 1.3b4d6.sit / OOFILE 1.3b4d6 / MacCodeWarriorDemo1.3b4d6 / docs / samples / ooftst07.h / ooftst07.h
Encoding:
Text File  |  1997-03-18  |  8.8 KB  |  273 lines  |  [TEXT/CWIE]

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST7.h
  4. // included in ooftest7 and 17
  5.  
  6. // this sample demonstrates searching and sorting by 
  7. // different types of fields, and how to parameterize
  8. // calls, passing in table and field references
  9.  
  10. // Simple stream I/O is used to interact with the user.
  11.  
  12.  
  13. DECLARE_CLASS(dbPeople)
  14.     dbChar        LastName, Othernames;
  15.     dbLong        Salary, SalaryNoDup;
  16.     dbShort        SalaryShort, SalaryShortNoDup;
  17.     dbReal        SalaryReal, SalaryRealNoDup;
  18.     dbDateTime    LastVisit, LastVisitNoDup;
  19.     dbUshort    ShortUnsigned;
  20.     dbUlong        LongUnsigned;
  21.  
  22.     dbPeople() :
  23.                 dbTable("People"),
  24.                 LastName(40, "Last Name"),
  25.                 Othernames(80, "Other names"),
  26.                 Salary("Salary"),
  27.                 SalaryNoDup("Salary NoDup"),
  28.                 SalaryShort("SalaryShort"),
  29.                 SalaryShortNoDup("SalaryShortNoDup"),
  30.                 SalaryReal("SalaryReal"),
  31.                 SalaryRealNoDup("SalaryRealNoDup"),
  32.                 LastVisit("LastVisit"),
  33.                 LastVisitNoDup("LastVisitNoDup"),
  34.                 ShortUnsigned("ShortUnsigned"),
  35.                 LongUnsigned("LongUnsigned")                
  36.     {
  37. #ifdef TEST_INDEXES
  38.             LastName.index(kIndexed);
  39.             Othernames.index(kIndexNoDups);
  40.             Salary.index(kIndexed);
  41.             SalaryNoDup.index(kIndexNoDups);
  42.             SalaryShort.index(kIndexed);
  43.             SalaryShortNoDup.index(kIndexNoDups);
  44.             SalaryReal.index(kIndexed);
  45.             SalaryRealNoDup.index(kIndexNoDups);
  46.             LastVisit.index(kIndexed);
  47.             LastVisitNoDup.index(kIndexNoDups);
  48.             ShortUnsigned.index(kIndexed);
  49.             LongUnsigned.index(kIndexed);                
  50. #endif
  51.     };
  52.     
  53. // my own data entry procedure
  54.     void Add(const char *lname, const char *oname, const long salary, const char* visitDate);
  55. };
  56.  
  57.  
  58.  
  59. void dbPeople::Add(const char *lname, const char *oname, const long salary, const char* visitDate)
  60. {
  61.     newRecord();
  62.     LastName = lname;
  63.     Othernames = oname;
  64.     Salary = salary;
  65.     SalaryNoDup = salary;
  66.     SalaryReal = salary;
  67.     SalaryRealNoDup = salary;
  68.     SalaryShort = salary;
  69.     SalaryShortNoDup = salary;
  70.     LastVisit = visitDate;
  71.     LastVisitNoDup = visitDate;
  72.     
  73.     ShortUnsigned = salary*5;  // numbers 10,000 to 49,999
  74.     LongUnsigned = ULONG_MAX - salary;  
  75.     
  76.     saveRecord();
  77. }
  78.  
  79. void testField(dbPeople& searchTable, dbNumericField& searchBy);
  80. void testField(dbPeople& searchTable, dbChar& searchBy, const char* searchAgainst, const char* searchUpto, const char* searchAgainstPartial);
  81. void testField(dbPeople& searchTable, dbDateTime& searchBy, const char* searchAgainst, const char* searchUpto);
  82. void doTest07(const char* existsName,const char* databaseName);
  83.  
  84.  
  85. void doTest07(const char* existsName,const char* databaseName)
  86. {
  87.     TEST_CONNECT    theDB;
  88.     dbPeople     People;
  89.  
  90.     if (dbConnect::fileExists(existsName)){
  91.         theDB.openConnection(databaseName);
  92.     }
  93.     else {
  94.         theDB.newConnection(databaseName);
  95.         People.Add("Smith", "John", 2000, "23/11/94");
  96.         People.Add("DENT", "Trissa", 9999, "22/11/94");
  97.         People.Add("Denton", "Andy", 5000, "23-1-95");
  98.         People.Add("Taylor", "Ken", 7500, "16,11,95");
  99.     }
  100. // simple numeric test first, then use the parameterized versions
  101. // Note the first few tests create temporary dbView's inline to output all records
  102. // in a single cout << blah;
  103.  
  104.     People.selectAll();
  105.     People.setSortOrder(People.LongUnsigned);
  106.     
  107.     cout << "Dumping database in key order by LongUnsigned (reverse of Salary)" << endl 
  108.          << (dbView(People) << People.LastName << People.Othernames << People.LongUnsigned) << endl << endl;
  109.  
  110.     cout << "Retrieving Andy Denton by ShortUnsigned == 25,000: " << endl;
  111.     People[People.ShortUnsigned.equal(25000)];
  112.     cout << (dbView(People) << People.LastName << People.Othernames << People.ShortUnsigned) << endl << endl;
  113.  
  114.     cout << "Finding LongUnsigned between " << ULONG_MAX-5001 << " and " << ULONG_MAX << endl;
  115.     People[People.LongUnsigned.between(ULONG_MAX-5001, ULONG_MAX)];
  116.     cout << (dbView(People) << People.LastName << People.Othernames << People.LongUnsigned) << endl << endl;
  117.  
  118.  
  119. // test char searches
  120.     testField(People, People.LastName, "Dent", "Denton", "DE");
  121.     testField(People, People.Othernames, "Andy", "Kell", "J");
  122.  
  123. // test numeric searches
  124.     testField(People, People.SalaryShort);
  125.     testField(People, People.SalaryShortNoDup);
  126.     testField(People, People.Salary);
  127.     testField(People, People.SalaryNoDup);
  128.     testField(People, People.SalaryReal);
  129.     testField(People, People.SalaryRealNoDup);
  130.     
  131. // test date searches
  132.     testField(People, People.LastVisit, "23/11/94", "23-1-95");
  133.     testField(People, People.LastVisitNoDup, "23/11/94", "22-1-95");
  134.     
  135.     cout << "done" << endl;
  136. }       
  137.  
  138. void testField(dbPeople& p, dbNumericField& searchBy)
  139. {    
  140.     p.selectAll();
  141.  
  142. // use a view to make it possible to dump just a few fields rather than the entire table
  143.     dbView fewFields(p);
  144.     fewFields << p.LastName << p.Othernames << searchBy;
  145.     
  146.     p.setSortOrder(searchBy);
  147.     cout << "Dumping database in key order by " << searchBy.fieldName() << endl 
  148.          << fewFields << endl << endl;
  149.     
  150.     cout << "Retrieving Andy Denton by " << searchBy.fieldName() << " = 5000: " << endl;
  151.     p[searchBy==5000];
  152.     cout << fewFields << endl << endl;
  153.  
  154.     cout << "Retrieving John Smith by " << searchBy.fieldName() << " < 5000: " << endl;
  155.     p[searchBy<5000];
  156.     cout << fewFields << endl << endl;
  157.  
  158.     cout << "Retrieving Smith & Andy Denton by " << searchBy.fieldName() << " <= 5000: " << endl;
  159.     p[searchBy<=5000];
  160.     cout << fewFields << endl << endl;
  161.  
  162.     cout << "Retrieving Taylor & Trissa Dent by " << searchBy.fieldName() << " > 5000: " << endl;
  163.     p[searchBy>5000];
  164.     cout << fewFields << endl << endl;
  165.  
  166.     cout << "Same search with >=5000 (should also get Andy Denton): " << endl;
  167.     p[searchBy>=5000];
  168.     cout << fewFields << endl << endl;
  169.  
  170.     cout << "Finding " << searchBy.fieldName() << " between 5000 and 7500 " << endl;
  171.     p[searchBy.between(5000,7500)];
  172.     cout << fewFields << endl << endl;
  173.  
  174.     cout << "Finding " << searchBy.fieldName() << " outside 5000 to 7500 " << endl;
  175.     p[searchBy.outside(5000,7500)];
  176.     cout << fewFields << endl << endl;
  177.  
  178.     
  179.     cout << "done" << endl;
  180. }
  181.  
  182.  
  183. void testField(dbPeople& p, dbChar& searchBy, const char* searchAgainst, const char* searchUpto, const char* searchAgainstPartial)
  184. {    
  185.     p.selectAll();
  186.  
  187.     dbView fewFields(p);
  188.     fewFields << p.LastName << p.Othernames << searchBy;
  189.     
  190.     p.setSortOrder(searchBy);
  191.     cout << "Dumping database in key order by " << searchBy.fieldName() << endl 
  192.          << fewFields << endl << endl;
  193.     
  194.     cout << "Finding " << searchBy.fieldName() << " starting with " << searchAgainstPartial << endl;
  195.     p[searchBy.startsWith(searchAgainstPartial)];
  196.     cout << fewFields << endl << endl;
  197.  
  198.     cout << "Finding " << searchBy.fieldName() << " == " << searchAgainst << endl;
  199.     p[searchBy==searchAgainst];
  200.     cout << fewFields << endl << endl;
  201.  
  202.     cout << "Finding " << searchBy.fieldName() << " < " << searchAgainst << endl;
  203.     p[searchBy<searchAgainst];
  204.     cout << fewFields << endl << endl;
  205.  
  206.     cout << "Finding " << searchBy.fieldName() << " <= " << searchAgainst << endl;
  207.     p[searchBy<=searchAgainst];
  208.     cout << fewFields << endl << endl;
  209.  
  210.     cout << "Finding " << searchBy.fieldName() << " > " << searchAgainst << endl;
  211.     p[searchBy>searchAgainst];
  212.     cout << fewFields << endl << endl;
  213.  
  214.     cout << "Finding " << searchBy.fieldName() << " >= " << searchAgainst << endl;
  215.     p[searchBy>=searchAgainst];
  216.     cout << fewFields << endl << endl;
  217.  
  218.     cout << "Finding " << searchBy.fieldName() << " between " 
  219.         << searchAgainst << " and " << searchUpto << endl;
  220.     p[searchBy.between(searchAgainst,searchUpto)];
  221.     cout << fewFields << endl << endl;
  222.  
  223.     cout << "Finding " << searchBy.fieldName() << " outside " 
  224.         << searchAgainst << " to " << searchUpto << endl;
  225.     p[searchBy.outside(searchAgainst,searchUpto)];
  226.     cout << fewFields << endl << endl;
  227.  
  228. }
  229.  
  230.  
  231. void testField(dbPeople& p, dbDateTime& searchBy, const char* searchAgainst, const char* searchUpto)
  232. {    
  233.     p.selectAll();
  234.     
  235.     dbView fewFields(p);
  236.     fewFields << p.LastName << p.Othernames << searchBy;
  237.     
  238.     p.setSortOrder(searchBy);
  239.     cout << "Dumping database in key order by " << searchBy.fieldName() << endl 
  240.          << fewFields << endl << endl;
  241.     
  242.     cout << "Finding " << searchBy.fieldName() << " == " << searchAgainst << endl;
  243.     p[searchBy==searchAgainst];
  244.     cout << fewFields << endl << endl;
  245.  
  246.     cout << "Finding " << searchBy.fieldName() << " < " << searchAgainst << endl;
  247.     p[searchBy<searchAgainst];
  248.     cout << fewFields << endl << endl;
  249.  
  250.     cout << "Finding " << searchBy.fieldName() << " <= " << searchAgainst << endl;
  251.     p[searchBy<=searchAgainst];
  252.     cout << fewFields << endl << endl;
  253.  
  254.     cout << "Finding " << searchBy.fieldName() << " > " << searchAgainst << endl;
  255.     p[searchBy>searchAgainst];
  256.     cout << fewFields << endl << endl;
  257.  
  258.     cout << "Finding " << searchBy.fieldName() << " >= " << searchAgainst << endl;
  259.     p[searchBy>=searchAgainst];
  260.     cout << fewFields << endl << endl;
  261.  
  262.     cout << "Finding " << searchBy.fieldName() << " between " 
  263.         << searchAgainst << " and " << searchUpto << endl;
  264.     p[searchBy.between(searchAgainst,searchUpto)];
  265.     cout << fewFields << endl << endl;
  266.  
  267.     cout << "Finding " << searchBy.fieldName() << " outside " 
  268.         << searchAgainst << " to " << searchUpto << endl;
  269.     p[searchBy.outside(searchAgainst,searchUpto)];
  270.     cout << fewFields << endl << endl;
  271.  
  272. }
  273.